﻿using IDAdmin.Common;
using IDAdmin.Lib.DataLayer;
using IDAdmin.Lib.UI;
using IDAdmin.Lib.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IDAdmin.Pages
{
    public partial class CreateCharacterStats : BasePage
    {
        public CreateCharacterStats()
            : base(Lib.AppFunctions.CREATECHARACTER)
        {
        }
        protected override void Page_Load(object sender, EventArgs e)
        {
            if (!IsLogin())
            {
                RedirectToLogOn();
            }
            else if (!CheckRight())
            {
                RedirectToDeniedMessage();
            }

        }

        protected void countButton_Click(object sender, EventArgs e)
        {
            dataPanel.Controls.Add(get_StatsCreate_charater(WebDB.getStatsCreateInCharBase()));
            measureLB.Controls.Add(get_StatsChoiceFade(WebDB.getStatsChoiceFaceInCharBase()));
        }

        private Table get_StatsChoiceFade(Dictionary<string, List<CharBase>> data)
        {
            List<string> serverList = new List<string>();
            serverList = data.Keys.ToList();
            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowTitle = new TableRow();
            rowTitle.Cells.AddRange
            (
              new TableCell[]
                {
                     UIHelpers.CreateTableCell("THỐNG KÊ CHỌN PHÁI",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader",2),
                 }
            );
            table.Rows.Add(rowTitle);

            string css;
            int stt = 0;
            int i = 0;

            try
            {
                foreach (var item in data)
                {
                    TableRow rowHeader = new TableRow();
                    rowHeader.Cells.AddRange
                    (
                        new TableCell[]
                        {                     
                            UIHelpers.CreateTableCell(item.Key,Unit.Percentage(5), HorizontalAlign.Center,"cell1",2),
                                       
                        }
                    );
                    //if ((stt + 1) == data.Count())
                    //{
                    table.Rows.Add(rowHeader);
                    //}

                    foreach (var face in item.Value)
                    {
                        i++;
                        css = i % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                               {
                                    UIHelpers.CreateTableCell(nameFace(Convert.ToInt32(face.face)),HorizontalAlign.Center,css),                               
                                    UIHelpers.CreateTableCell(Converter.ToString(face.totalChoiceFace),HorizontalAlign.Center,css),                                   
                               }
                        );
                        table.Rows.Add(row);
                    }
                    stt++;
                }

            }
            catch (Exception ex)
            {

            }
            return table;
        }

        private Table get_StatsCreate_charater(Dictionary<string, List<CharBase>> data)
        {
            List<string> serverList = new List<string>();
            serverList = data.Keys.ToList();
            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowTitle = new TableRow();
            rowTitle.Cells.AddRange
           (
               new TableCell[]
                {
                     UIHelpers.CreateTableCell("THỐNG KÊ SỐ LƯỢNG TÀI KHOẢN TẠO MỚI",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader",3),
                }
           );
            table.Rows.Add(rowTitle);
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
             (
                 new TableCell[]
                {                        
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Server",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số lượng tài khoản tạo mới",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),                   
                }
             );
            string css;
            int stt = 0;
            table.Rows.Add(rowHeader);
            try
            {
                foreach (var server in serverList)
                {
                    stt++;
                    css = stt % 2 == 0 ? "cell1" : "cell2";
                    TableRow row = new TableRow();
                    row.Cells.AddRange
                    (
                        new TableCell[]
                           {
                                UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                UIHelpers.CreateTableCell(server.ToString(),HorizontalAlign.Center,css),
                                UIHelpers.CreateTableCell(Converter.ToString(data.ElementAt(stt - 1).Value.ElementAt(0).totalCreateCharacter),HorizontalAlign.Center,css),                                   
                           }
                    );
                    table.Rows.Add(row);
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
            return table;
        }

        ///list face name

        static private string nameFace(int key)
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            if (key == 9)
            {
                key = 5;
            }
            else if (key == 10)
            {
                key = 6;
            }
            list.Add("0", "Không chọn phái");
            list.Add("1", "Thiên Vũ");
            list.Add("2", "Linh Vũ");
            list.Add("3", "Huyền Thuật");
            list.Add("4", "Tiên Âm");
            list.Add("9", "U Liêm Ngoại");
            list.Add("10", "U Liêm Nội");
            return list.ElementAt(key).Value;
        }
    }
}